//
//  VisitorView.swift
//  myWeibo
//
//  Created by MrWu on 16/4/7.
//  Copyright © 2016年 MrWu. All rights reserved.
//

import UIKit

protocol visitorViewDelegate: NSObjectProtocol {
    func registerButtonClick()
    func loginButtonClick()
}


class VisitorView: UIView {
    
    var delegate: visitorViewDelegate?
    
    func regButtonDidClick() {
        delegate?.registerButtonClick()
    }
    func logButtonDidClick() {
        delegate?.loginButtonClick()
    }

    func setupInfo(imageName:String?,title:String) {
        messageLabel.text = title
        guard let imgName = imageName else {
            startAnmi()
            maskiconView.hidden = false
            bringSubviewToFront(maskiconView)
         
            return
        }
        iconView.image = UIImage(named: imgName)
        houseiconView.hidden = true
        sendSubviewToBack(maskiconView)
    }
    
    private func startAnmi() {
        let anmi = CABasicAnimation(keyPath: "transform.rotation")
        anmi.repeatCount = MAXFLOAT
        anmi.duration = 20
        anmi.toValue = 2 * M_PI
        //layer消失,动画自动销毁的方法
        anmi.removedOnCompletion = false
        iconView.layer .addAnimation(anmi, forKey: nil)
        
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    /**
    *  @author WuDi
    *
    *  懒加载
    */
    
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    private lazy var maskiconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var houseiconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerButton:UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    private lazy var loginButton:UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
}

extension VisitorView {
    
    
    private func setupUI() {
        //添加控件
        addSubview(iconView)
        addSubview(maskiconView)
        addSubview(houseiconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //swift的自动布局
        //translatesAutoresizingMaskIntoConstraints 默认是true用setframe方式设置false用约束
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute:NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(item: houseiconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseiconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        //两个按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //mask
        //VFL可视化语言
//        H 水平方向
//        V 垂直方向
//            | 边界
//        [] 包装控件
//        views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
//        metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskiconView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight":60], views: ["mask":maskiconView, "regButton": registerButton]))
        
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
        loginButton.addTarget(self, action: "logButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        registerButton.addTarget(self, action: "regButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    
}
